const app = getApp();
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
import {
  ApiUserRealNameCheck
} from '../../api/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl,
    commonUpdate,
    commonUpload,
    imgPath: '',
    name: '',
    age: '',
    sex: '',
    idcard: '',
    phone: '',
    hospitalId: '',
    account: '',
    address: '',
    cWidth: '',
    cHeight: '',
    ops: {
      card_pic: '',
      patient_id:'',
      type: 'user'
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      userInfo: JSON.parse(options.data)
    })
    /*   console.log('options===', options); */
    /* if (options.q) {
      let url = decodeURIComponent(options.q);
      let search = url.match(/\?(.*)$/);
      let arr1 = search[1].split('&');
      let obj = {};
      for (let item of arr1) {
        let arr2 = item.split('=');
        obj[arr2[0]] = arr2[1];
      }
      console.log(obj);
      this.setData({
        hospitalId: obj.hospitalId,
        account: obj.account
      });
    } */
  },


  /*   afterRead: function (event) {
      console.log(event.file)
      let that = this
      let json = JSON
      const {
        file,
      } = event.detail
      this.setData({
        type: file.type
      })

      wx.showLoading({
        title: '上传中',
        mask: true
      })
      wx.uploadFile({
        url: baseUrl + commonUpdate,
        filePath: file.url,
        name: 'file',
        header: {
          Authorization: 'Bearer ' + wx.getStorageSync('token')
        },
        success(res) {
          let baseUrlLink = baseUrl + commonUpload + json.parse(res.data).data.path
          that.setData({
            'ops.icon': baseUrlLink
          })

          wx.showToast({
            title: '上传文件成功',
          })
        }
      })
    },
   */







  /**
   * 选择图片上传
   */
  choosePic: function (e) {
    var that = this;
    let json = JSON;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.uploadFile({
          url: baseUrl + commonUpdate,
          filePath: res.tempFiles[0].path,
          header: {
            Authorization: 'Bearer ' + wx.getStorageSync('token')
          },
          name: 'file',
          success(res) {
            let baseUrlLink = json.parse(res.data).data.path
            let icon = baseUrl + commonUpload + json.parse(res.data).data.path
            that.setData({
              'ops.card_pic': baseUrlLink,
              'ops.icon': icon
            })
            wx.showToast({
              title: "上传成功",
            })
          },
          fail(err) {
            console.log(err)
          }
        })
        console.log(res);
        /*    var size = res.tempFiles[0].size / 1024;
           console.log('图片大小=', size + 'k');
           wx.getImageInfo({
             src: res.tempFilePaths[0],
             success: function (res) {
               console.log(res);
             }
           }); */
        /*    if (size > 1024) {
             that.getImgBase64(res.tempFilePaths[0]);
           } else {
             that.getImgBase64(res.tempFilePaths[0]);
           } */
      }
    });
  },

  /**
   * 识别身份证
   */
  /*   identifyIdCard: function (base64Str) {
      wx.showLoading({
        title: '身份证识别中',
      });
      var that = this;
      wx.request({
        url: app.globalData.thirdApiPrefix + 'api/idcard', //这里换上你们自己的身份证识别地址
        data: {
          imgBase64: base64Str,
          idcardSide: 'front'
        },
        method: 'post',
        header: {
          'content-type': "application/x-www-form-urlencoded"
        },
        dataType: 'json',
        success: function (ret) {
          wx.hideLoading();
          if (ret && ret.data) {
            if (ret.data.image_status === 'normal') {
              that.setData({
                name: ret.data.words_result['姓名'].words,
                sex: ret.data.words_result['性别'].words,
                age: new Date().getFullYear() - ret.data.words_result['出生'].words.substr(0, 4),
                idcard: ret.data.words_result['公民身份号码'].words,
                address: ret.data.words_result['住址'].words,
                imgPath: 'data:image/png;base64,' + base64Str
              });
              wx.showToast({
                title: '识别成功，请确认信息是否正确',
                icon: 'none',
                duration: 3000
              });
            } else {
              wx.showToast({
                title: '无法识别，请重新上传',
                icon: 'none',
                duration: 1500
              });
            }
          } else {
            wx.showToast({
              title: '服务异常',
              icon: 'none',
              duration: 1500
            });
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res);
          wx.showToast({
            title: '网络故障，请重试',
            icon: 'none',
            duration: 1500
          });
        },
        complete: function (res) {},
      });
    }, */
  /**
   * 跳转更多信息页面
   */
  openNextPage() {
    delete this.data.ops.icon

    this.data.ops.patient_id = wx.getStorageSync('userInfo').id
    if(!this.data.ops.patient_id){
      wx.removeStorage({
        key: 'token',
      })
    }
    ApiUserRealNameCheck(this.data.ops).then(res => {
      if (res.code == '200') {
        //更新身份证号
        let userInfo = wx.getStorageSync('userInfo')
        userInfo.card_no = res.data
        wx.setStorageSync('userInfo', userInfo)
        wx.showToast({
          title: "认证成功",
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 2000);
      }
    })
  },

  /**
   * 输入手机号绑定
   */



  /* checkIdcard(e) {
    if (e.detail.value && !(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(e.detail.value))) {
      wx.showToast({
        title: '身份证格式错误',
        icon: 'none',
        duration: 1500
      });
    }
  }, */
  /**
   * 选择性别
   */
  /*  radioChange(e) {
     let that = this;
     console.log('性别===', e.detail.value);
     that.setData({
       sex: e.detail.value
     });
   }, */
})